package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.enterprise.AddInformationRequest;
import com.springboot_gradrecruit.model.dto.enterprise.PageInformationRequest;
import com.springboot_gradrecruit.model.dto.enterprise.UpdateInformationRequest;
import com.springboot_gradrecruit.model.entity.Enterprise;
import com.springboot_gradrecruit.model.vo.enterprise.EnterpriseVO;
import com.springboot_gradrecruit.service.EnterpriseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zr
 */
@Tag(name = "企业相关接口")
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {

    @Resource
    private EnterpriseService enterpriseService;

    //增加
    @Operation(summary = "添加企业信息")
    @PostMapping("/addInformation")
    public R<Boolean> addInformation(@RequestBody AddInformationRequest addInformationRequest, HttpServletRequest request){
        // 1. 参数校验
        if(addInformationRequest ==null||request==null){
            throw new RuntimeException("参数为空");
        }
        // 2. 调用service完成添加
        Long aLong=enterpriseService.addInformation(addInformationRequest, request);
        if(aLong>0){
            return R.success(true, "添加成功，已提交审核！");
        }
        return R.error(false, "添加失败");

    }

    //修改
    @Operation(summary = "修改企业信息通过企业ID")
    @PostMapping("/updateInformationById")
    public R<Boolean> updateInformationById(@RequestBody UpdateInformationRequest updateInformationRequest, HttpServletRequest request) {
        //1. 参数校验
        if (updateInformationRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        boolean updateJob = enterpriseService.updateInformationById(updateInformationRequest);
        if (updateJob) {
            return R.success(null, "修改成功");
        }
        return R.error(null, "修改失败");
    }


    //查询
    @Operation(summary = " 查询企业信息通过企业绑定的UserID")
    @PostMapping("/queryInformationByUserId")
    public R<EnterpriseVO> queryInformationByUserId(Long enterpriseUserId){
        // 1. 参数校验
        if(enterpriseUserId==null){
            throw new RuntimeException("参数为空");
        }
        // 2. 调用service完成查询
        EnterpriseVO enterpriseVO=enterpriseService.queryInformationByUserId(enterpriseUserId);
        return R.success(enterpriseVO, "查询成功");

    }


    @Operation(summary = "查询企业信息通过企业ID")
    @PostMapping("/queryInformationById")
    public R<EnterpriseVO> queryInformationById(Long enterpriseId){
        // 1. 参数校验
        if(enterpriseId==null){
            throw new RuntimeException("参数为空");
        }
        // 2. 调用service完成查询
        Enterprise enterprise=enterpriseService.getById(enterpriseId);
        EnterpriseVO enterpriseVO= BeanUtil.copyProperties(enterprise, EnterpriseVO.class);
        return R.success(enterpriseVO, "查询成功");

    }

    @Operation(summary = "分页获取企业信息")
    @PostMapping("/pageInformation")
    public R<PageResponse<EnterpriseVO>> pageInformation(@RequestBody PageInformationRequest pageInformationRequest, HttpServletRequest request) {
        //1. 参数校验
        if (pageInformationRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        PageResponse<EnterpriseVO> pageInformation = enterpriseService.pageInformation(pageInformationRequest);
        return R.success(pageInformation, "查询成功");
    }

    //学生首页获取热门公司
    @Operation(summary = "学生首页获取热门公司")
    @PostMapping("/queryHotEnterprise")
    public R<List<EnterpriseVO>> queryHotEnterprise(){
        //1. 调用service完成查询
        List<EnterpriseVO> enterpriseVOList = enterpriseService.queryHotEnterprise();
        return R.success(enterpriseVOList, "查询成功");
    }
}
